package com.lc.qianrenshiye.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.Conn;
import com.lc.qianrenshiye.conn.OrderPriceGet;
import com.lc.qianrenshiye.conn.WXPayGet;
import com.lc.qianrenshiye.dialog.CutPriceDialog;
import com.lc.qianrenshiye.uitls.MyAnimationUitls;
import com.lc.qianrenshiye.uitls.MyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

/**
 * Created by Administrator on 2016/11/18.
 */
public class ConfirmOrderActivity extends BaseActivity implements View.OnClickListener, View.OnTouchListener, GestureDetector.OnGestureListener{


    @BoundView(R.id.confirm_order_ll) private LinearLayout confirm_order_ll;
    @BoundView(R.id.confirm_order_need_pay_tv) private TextView confirm_order_need_pay_tv;
    @BoundView(R.id.confirm_order_orig_tv) private TextView confirm_order_orig_tv;
    @BoundView(R.id.confirm_order_weixin_rl) private RelativeLayout confirm_order_weixin_rl;
    @BoundView(R.id.confirm_order_alipay_rl) private RelativeLayout confirm_order_alipay_rl;
    @BoundView(R.id.confirm_order_pay_type_tv) private TextView confirm_order_pay_type_tv;
    @BoundView(R.id.confirm_order_pay_tv) private TextView confirm_order_pay_tv;

    private GestureDetector detector;

    private int payType; //0 微信支付， 1 支付宝

    private String amount, oid, ordersn;

    private OrderPriceGet orderPriceGet = new OrderPriceGet(new AsyCallBack<OrderPriceGet.OrderPriceEntity>() {
        @Override
        public void onSuccess(String toast, int type, OrderPriceGet.OrderPriceEntity orderPriceEntity) throws Exception {

            amount = orderPriceEntity.amount;

            ordersn = orderPriceEntity.ordersn;

            //需支付
            confirm_order_need_pay_tv.setText(amount);

            //设置删除线
            confirm_order_orig_tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            confirm_order_orig_tv.getPaint().setAntiAlias(true);
            //原价  打折后才显示
            confirm_order_orig_tv.setText(getString(R.string.orig_text) + amount + getString(R.string.my_order_money_unit__text));

            MyAnimationUitls.toVisibleAnim(confirm_order_ll);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);

        }
    });

    private WXPayGet wxPayGet = new WXPayGet(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            Bundle bundle = new Bundle();

            bundle.putString("PayMoney", wxPayGet.total_fee);
            bundle.putString("qrcode", s);
            bundle.putInt("PayType", payType);

            startActivity(new Intent(context, PayActivity.class).putExtras(bundle));

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);

        }
    });

    private String mCutType = "0";
    private String mFullcut = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order);

        setBackTrueOrder();

        setTitleNameOrder(getString(R.string.confirm_order_title_text));

        oid = getIntent().getExtras().getString("oid");

        Log.e("---", "oid" + oid);

        orderPriceGet.oid = oid;
        orderPriceGet.execute(this);


        //下面都是 手势设置监听
        confirm_order_ll.setOnTouchListener(this);

        //下面两个要记得设哦，不然就没法处理轻触以外的事件了，例如抛掷动作。

        confirm_order_ll.setLongClickable(true);

        detector = new GestureDetector(this);

        detector.setIsLongpressEnabled(true);

        //按钮监听
        confirm_order_pay_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.confirm_order_weixin_rl:

                payType = 0;

                confirm_order_weixin_rl.setBackgroundResource(R.drawable.rectangle_red_3px_stroke);
                confirm_order_alipay_rl.setBackgroundResource(R.drawable.rectangle_grey_3px_stroke);

                confirm_order_pay_type_tv.setText(getString(R.string.weixin_pay_text));

                break;

            case R.id.confirm_order_alipay_rl:

                payType = 1;

                confirm_order_alipay_rl.setBackgroundResource(R.drawable.rectangle_red_3px_stroke);
                confirm_order_weixin_rl.setBackgroundResource(R.drawable.rectangle_grey_3px_stroke);

                confirm_order_pay_type_tv.setText(getString(R.string.alipay_text));

                break;

            case R.id.confirm_order_pay_tv:

                Bundle bundle = new Bundle();

                String payMoney ;
                String url;

                switch (payType) {

                    case 0:

                        payMoney = confirm_order_need_pay_tv.getText().toString();

                        wxPayGet.ordersn = ordersn;
                        wxPayGet.discount = mFullcut;
                        wxPayGet.total_fee = payMoney;
                        wxPayGet.type = mCutType;

                        wxPayGet.execute(this);


                        break;

                    case 1:

                        payMoney = confirm_order_need_pay_tv.getText().toString();
                        url = Conn.SERVICE + Conn.ALIPAY_DOALIPAY + "?ordersn=" + ordersn + "&amount=" + amount + "&type=" + mCutType + "&discount=" + mFullcut + "&total_fee=" + payMoney + "&subject=" + ordersn;

                        Log.e("url", url);

                        bundle.putString("PayMoney", payMoney);
                        bundle.putInt("PayType", payType);
                        bundle.putString("url", url);

                        startActivity(new Intent(this, PayActivity.class).putExtras(bundle));

                        break;
                }


                break;
        }
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {


//        if (e1.getX() - e2.getX() > 20 && Math.abs(velocityX) > 200 ) {
//
//            MyToast.showToast("向左", context);
//
//        } else if (e2.getX() - e1.getX() > 20 && Math.abs(velocityX) > 200) {
//
//            MyToast.showToast("向右", context);
//
//        }
//        else

//        if (e1.getY() - e2.getY() > 100 && Math.abs(velocityY) > 200) {
//
//
//            new NotPayDialog(context, this).show();
//
//
//        } else

        if (e2.getY() - e1.getY() > 100 && Math.abs(velocityY) > 200) {

            Log.e("----", "向下");

            new CutPriceDialog(context, amount) {

                @Override
                public void onConfirm(String price, String cutPrice, String cutType, String fullcut) {

                    Log.e("----", "price" + price + "cutPrice" + cutPrice + "cutType" + cutType + "fullcut" + fullcut);

                    mCutType = cutType;
                    mFullcut = fullcut;

                    if (!cutPrice.equals(amount)) {

                        confirm_order_orig_tv.setVisibility(View.VISIBLE);

                    } else {

                        confirm_order_orig_tv.setVisibility(View.GONE);

                    }

                    confirm_order_need_pay_tv.setText(cutPrice);

                    dismiss();

                }

            }.show();
        }


        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        detector.onTouchEvent(event);

        return true;
    }


}
